package com.xiaodou.mjf.web.console.basic.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.xiaodou.mjf.application.basic.service.OperatorService;
import com.xiaodou.mjf.application.basic.service.RoleService;
import com.xiaodou.mjf.application.basic.utils.OperatorUtils;
import com.xiaodou.mjf.entity.basic.Operator;
import com.xiaodou.mjf.entity.basic.Role;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;
import com.xiaodou.mjf.infrastructure.common.utils.Digests;
import com.xiaodou.mjf.infrastructure.common.utils.Encodes;
import com.xiaodou.mjf.infrastructure.common.utils.Encrypt;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;

@Controller
@RequestMapping("operator")
public class OperatorController extends ApplicationController {

	@Resource
	OperatorService operatorService;

	@Resource
	RoleService roleService;

	@RequestMapping(value = { "", "list" })
	public String list(Model model) {

		List<Operator> operators = operatorService.list();
		Operator platform = OperatorUtils.getOperator();
		if(operators!=null) {
			if(platform.getRoles().get(0).getPlatform()!=0) {
				for (int i = operators.size()-1; i >= 0; i--) {
//					if(!operators.get(i).getMerchantId().equals(platform.getMerchantId())) {
//						operators.remove(i);
//					}
					if(operators.get(i).getRoles().get(0).getPlatform()!=platform.getRoles().get(0).getPlatform()||!platform.getMerchantId().equals(operators.get(i).getMerchantId())){
						operators.remove(i);
					}
				}
			}
		}
		model.addAttribute("operators", operators);

		return "security/operator/list";
	}

	@RequestMapping(value = { "form", "form/{id}" })
	public String form(Operator operator, Model model) {
		if (StringUtils.isNotBlank(operator.getId())) {
			operator = operatorService.get(operator.getId());
		}
		List<Role> roleList = roleService.list();
		Operator platform = OperatorUtils.getOperator();
		if(roleList!=null) {
			if(platform.getRoles().get(0).getPlatform()!=0) {
				for (int i = roleList.size()-1; i >= 0; i--) {
					if(roleList.get(i).getPlatform()!=platform.getRoles().get(0).getPlatform()){
						roleList.remove(i);
					}
				}
			}
		}
		operator.getRoleIdList();
		operator.setMerchantId(platform.getMerchantId());
		model.addAttribute("operator", operator);
		model.addAttribute("allRoles", roleList);
		return "security/operator/form";
	}

	@RequestMapping("save")
	public String save(Operator operator, Model model) {
		if (!beanValidator(model, operator)) {
			return form(operator, model);
		}
		// 角色数据有效性验证，过滤不在授权内的角色
		List<Role> roleList = Lists.newArrayList();
		List<String> roleIdList = operator.getRoleIdList();
		for (Role r : roleService.list()) {
			if (roleIdList.contains(r.getId())) {
				roleList.add(r);
			}
		}
		operator.setRoles(roleList);
		operator.setPassword(Encrypt.entryptPassword(operator.getPassword()));
		operatorService.save(operator);
		return "redirect:/operator/list";
	}

	/**
	 * 修改登陆密码
	 * 
	 * @param id
	 * @param oldPwd
	 * @param newPwd
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "updatePassword", "updatePassword/{id}" })
	@ResponseBody
	public Map<String, Object> updatePassword(@PathVariable String id, String oldPwd, String newPwd) {
		Operator operator = operatorService.get(id);
		if (operator == null) {
			return map(false, "该用户名不存在");
		}
		byte[] salt = Encodes.decodeHex(operator.getPassword().substring(0, 16));
		byte[] hashPassword = Digests.sha1(oldPwd.getBytes(), salt, 1024);
		boolean flag = operator.getPassword().equals(Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword));
		if (!flag) {
			return map(false, "原始密码不正确");
		}
		operator.setPassword(Encrypt.entryptPassword(newPwd));
		operatorService.save(operator);
		return map(true, "修改成功");
	}
}
